package cn.topevery.garden.dal.dao.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.topevery.garden.dal.dao.ScheduleDao;
import cn.topevery.garden.entity.dto.ScheduleDTO;
import cn.topevery.garden.entity.po.Schedule;
import cn.topevery.garden.entity.query.ScheduleQuery;
import cn.topevery.garden.entity.vo.ScheduleVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author code-generate
 * @date 2022-02-16
 */
@Repository
public class ScheduleDaoImpl extends BaseDaoImpl<Schedule> implements ScheduleDao {
    @Override
    public <P extends DbCsp> List<Schedule> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<ScheduleVO> listPage(ScheduleQuery query) {
        String table = " t_schedule ts left join t_plan t on ts.c_schedule_id = t.c_plan_id";

        String columns = " DISTINCT ts.*,t.c_section_name";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("ts.c_id desc");
        sqlQueryMate
                .where(true, "ts.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getFlowState()), "ts.c_flow_state", ScriptConditionEnum.Eq, query.getFlowState())
                .where(query.getType()!=null, "ts.c_type", ScriptConditionEnum.Eq, query.getType())
                .where(ObjectUtil.isNotEmpty(query.beginDate)&&ObjectUtil.isNotEmpty(query.endDate), "ts.c_end_date", ScriptConditionEnum.GreaterThanEq, query.beginDate)
                .where(ObjectUtil.isNotEmpty(query.beginDate)&&ObjectUtil.isNotEmpty(query.endDate), "ts.c_end_date", ScriptConditionEnum.LessThanEq, query.endDate)

        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, ScheduleVO.class);
    }

    @Override
    public Schedule getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public DataPager<ScheduleVO> getSampleRecord(ScheduleQuery query) {
        String table = " t_schedule ts left join t_plan t on ts.c_schedule_id = t.c_plan_id";

        String columns = " DISTINCT ts.*,t.c_section_name";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        List<Integer> recordType = new ArrayList<>(2);
        recordType.add(3);
        recordType.add(4);
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("ts.c_db_last_update_date desc");
        sqlQueryMate
                .where(true, "ts.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(query.getType()==5, "ts.c_type", ScriptConditionEnum.In, recordType)
                .where(StringUtils.isNotBlank(query.getScheduleName()), "ts.c_schedule_name", ScriptConditionEnum.Like, query.getScheduleName())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, ScheduleVO.class);
    }

    public List<Map<String, Object>> getMonth() {
        String sql= "SELECT COUNT(*) val,MONTH(c_end_date) `name`,\"件\" unit FROM  t_schedule WHERE c_end_date >= DATE_FORMAT(NOW(),'%Y-01-01') and c_flow_state =2 GROUP BY MONTH(c_end_date)";
        return getMap(sql);
    }


    public void updateType(ScheduleDTO dto) {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_schedule");
        sqlUpdateMate.setColumnValue(true, "c_type", dto.getType())
                .setColumnValue(true, "c_sample_desc", dto.getSampleDesc())
                .where(true, "c_id", ScriptConditionEnum.In, dto.getIds());
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }
}
